<?php
namespace Lib\Finance;
/**
 * 处理雇主用户的财务逻辑
 *
 * @author chacha
 * @date 2016-6-2
 */
class Cop {

    protected $uid = 0;
    protected $team = 0;

    public function __construct($uid){
        $this->uid = $uid;
        $field = "team.*,prj.id as pid";
        $join = "LEFT JOIN xia_project prj ON prj.tid=team.id";
        $team = M()
        ->table(array('xia_team'=>'team'))
        ->field($field)
        ->join($join)
        ->where("team.uid=".$this->uid." and team.state=1")
        ->find();
        if ($team) {
            $this->team = $team;
        }
    }

    //获取团队
    public function getTeam(){
        return $this->team;
    }

    //获取托管冻结数额
    public function getTuoguan() {
        //return 120;
        if ($this->team) {
            $sql1 = "SELECT sum(j.salary) AS total"
                        ." FROM xia_job AS j"
                        ." WHERE j.pid=".$this->team['pid']." and j.moneyin=".\App\Common\Constvars::JOB_SALARYSTATUS_DG." and j.instalment=".\App\Common\Constvars::JOB_DGTYPE_SINGLE;
            //echo $sql1;
            $totalArr1 = M()->query($sql1);
            $sql2 = "SELECT sum(xis.amount) AS total"
                        ." FROM xia_instal_stage xis"
                        ." WHERE xis.moneyin=".\App\Common\Constvars::JOB_SALARYSTATUS_DG." and xis.jobid IN"
                        ." (SELECT j.id FROM xia_job j,xia_project prj WHERE j.pid=prj.id and prj.tid=".$this->team['id'].")";
            //echo $sql2;
            //exit;
            $totalArr2 = M()->query($sql2);
            $total = $totalArr1[0]['total']+$totalArr2[0]['total'];
            return $total;
        }
        else {
            return 0;
        }
    }
    
    //获取财务提醒消息
    public function getTips() {
        $sql = "SELECT * FROM xia_finance_tip xis WHERE uid=".$this->uid." and status=1";
        $tips = M()->query($sql);
        return $tips;
    }

    //获取进行中职位
    public function getDoingjob(){
        $field = "j.id,j.name as jname,j.salary,j.moneyin,j.instalment,ju.jointime,ju.income";
        $tables = array('xia_job'=>'j');
        $join1 = "LEFT JOIN xia_job_user ju ON ju.jobid=j.id";
        //$join2 = "LEFT JOIN xia_team team ON team.uid=".$this->uid;
        $jobs = M('job')
        ->table($tables)
        ->field($field)
        ->join($join1)
        ->where("j.pid=".$this->team['pid']." and j.state=". \App\Common\Constvars::STATUS_NORMAL)
        ->order("ctime desc")
        ->select();
//        echo $jobs;
//        exit;
        if (is_array($jobs)) {
            foreach ($jobs as $i => $job) {
                if ($job['instalment']==2) {
                    $instals = M('instal_stage')->where("jobid=".$job['id'])->select();
                    if ($instals) {
                        $job['instals'] = $instals;
                    }
                    else {
                        $job['instals'] = false;
                    }
                    $jobs[$i] = $job;
                }
            }
        }
//        dump($jobs);
//        exit;
        return $jobs;
    }
    
    //取消充值申请
    public function cancelcharge($cid) {
        $ret = M('charge_log')->where("id=$cid and uid=".$this->uid)->save(array('process'=> \App\Common\Constvars::CHARGE_PRO_CANCEL));
        if ($ret===false) {
            return false;
        }
        else {
            return true;
        }
    }
    
    //删除充值申请
    public function delcharge($cid) {
        $ret = M('charge_log')->where("id=$cid and uid=".$this->uid)->delete();
        if ($ret===false) {
            return false;
        }
        else {
            return true;
        }
    }
}
